package com.beta.resource.utils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BaseString {
    private static final String DEFAULT_BASE_STRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final String DEFAULT_CHARSET_NAME = "utf-8";

    public static String decodeBase64(String src, String defval) {
        String dst;
        try {
            if (src == null || src.isEmpty()) {
                dst = defval;
            } else {
                dst = new String(Base64.getDecoder().decode(src), DEFAULT_CHARSET_NAME);
            }
        } catch (UnsupportedEncodingException e) {
            dst = defval;
            e.printStackTrace();
        }
        return dst;
    }

    public static String encodeBase64(String src, String except) {
        String dst = null;
        try {
            dst = encodeBase64(src.getBytes(DEFAULT_CHARSET_NAME));
        } catch (UnsupportedEncodingException e) {
            dst = except == null ? src : except;
            e.printStackTrace();
        }
        return dst;
    }

    public static String encodeBase64(byte[] src) {
        return Base64.getEncoder().encodeToString(src);
    }

    public static String subString(String src, int limitLength) {
        int limitByteLength = (limitLength / 4 - 1) * 3;
        String dest = "";
        if (src.trim().getBytes().length > limitByteLength) {
            for (int i = 0; i < src.length(); i++) {
                char ch = src.charAt(i);
                limitByteLength -= String.valueOf(ch).getBytes().length;
                if (limitByteLength >= 0) {
                    dest += ch;
                } else {
                    break;
                }
            }
        } else {
            dest = src;
        }
        return dest;
    }

    public static boolean isBase64(String src) {
        String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
        return Pattern.matches(base64Pattern, src);
    }

    public static boolean isChinese(char c) {
        Character.UnicodeScript sc = Character.UnicodeScript.of(c);
        if (sc == Character.UnicodeScript.HAN) {
            return true;
        }
        return false;
    }

    public static String getRandomString(int length) {
        Random random = new Random();
        String ret = "";
        for (int i = 0; i < length; i++) {
            ret += DEFAULT_BASE_STRING.charAt(random.nextInt(DEFAULT_BASE_STRING.length()));
        }
        return ret;
    }

    public static boolean isUrl(String str) {
        boolean isurl = false;
        String regex = "(((https|http)?://)?([a-z0-9]+[.])|(www.))"
                + "\\w+[.|\\/]([a-z0-9]{0,})?[[.]([a-z0-9]{0,})]+((/[\\S&&[^,;\u4E00-\u9FA5]]+)+)?([.][a-z0-9]{0,}+|/?)";
        Pattern pat = Pattern.compile(regex.trim());
        Matcher mat = pat.matcher(str.trim());
        isurl = mat.matches();
        if (isurl) {
            isurl = true;
        }
        return isurl;
    }

    public static String translateEncode(byte[] str, int length, String charset) {
        return translateEncode(new String(str, 0, length), charset);
    }

    public static String translateEncode(String str, String charset) {
        String ret = str;
        String encode = getEncoding(str);
        if (!encode.isEmpty() && !encode.equals(charset)) {
            try {
                ret = new String(str.getBytes(encode), charset);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                ret = str;
            }
        }
        return ret;
    }

    public static String getEncoding(String str) {
        String[] charsets = { "UTF-16", "ASCII", "GB2312", "ISO-8859-1", "GBK", "UTF-8" };
        for (String charset : charsets) {
            if (isEncoding(str, charset)) {
                return charset;
            }
        }
        return "";
    }

    public static boolean isEncoding(String str, String encode) {
        boolean ret = false;
        try {
            ret = str.equals(new String(str.getBytes(), encode));
        } catch (UnsupportedEncodingException e) {
            ret = false;
        }
        return ret;
    }
}
